package com.example.controller;

import com.example.pojo.User;
import com.google.gson.Gson;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

// @Controller
// @ResponseBody
@RestController
public class GsonController {
    @RequestMapping("/json1")
    public String json1() {
        User user = new User("小明", 3, "男");
        Gson gson = new Gson();
        String json = gson.toJson(user);
        return json;
    }

    @RequestMapping("/json2")
    public String json2() {
        User user1 = new User("小明1", 3, "男");
        User user2 = new User("小明2", 3, "男");
        User user3 = new User("小明3", 3, "男");
        User user4 = new User("小明4", 3, "男");

        List<User> userList = new ArrayList<User>();
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);

        Gson gson = new Gson();
        String json = gson.toJson(userList);
        return json;
    }

    @RequestMapping("/json3")
    public String json3() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
        Gson gson = new Gson();
        String json = gson.toJson(simpleDateFormat.format(date));
        return json;
    }
}
